﻿using ProgrammersCalculator.Model;
using System;
using System.Windows.Data;


namespace ProgrammersCalculator.Convertor
{
    public class HashConvertor : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                // Get the source value
                HashAlgorithmWrapper algo = new HashAlgorithmWrapper();
                
                string data = value.ToString();
                if (data == "")
                    return null;

                string encodeDecode = parameter.ToString();
                if (encodeDecode.Equals("md5"))
                    return algo.ComputeHash(data, Common.AlgorithmType.MD5);
                if (encodeDecode.Equals("sha1"))
                    return algo.ComputeHash(data, Common.AlgorithmType.SHA1);
                if (encodeDecode.Equals("sha256"))
                    return algo.ComputeHash(data, Common.AlgorithmType.SHA256);
                if (encodeDecode.Equals("sha512"))
                    return algo.ComputeHash(data, Common.AlgorithmType.SHA512);
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
